package br.edu.ufam.controleAcesso

class Menu {
    
    static auditable = true

    static mapping = {
        id generator:'uuid'
    }

    String id
    String descricao
    Date dateCreated
    Date lastUpdated
    Usuario usuarioCreated
    Usuario usuarioUpdated
    
    static constraints = {
        descricao(blank:false, maxSize:25, unique:true)
    }

    String toString(){
        "${descricao}"
    }

    boolean equals(o) {
        if (this.is(o))
            return true;
        if (!o || getClass() != o.class)
            return false;
        Menu menu = (Menu) o;
        if (this.id ? !this.id.equals(menu.id) : menu.id != null) return false;
        return true;
    }

    int hashCode() {
        return (this.id ? this.id.hashCode() : 0);
    }

    public List getMenus(){
        Set lista = [] as Set
        Menu.executeQuery("select g.aplicacao.menu from GrupoAplicacao g where g.grupo.id = ?", [this.grupo.id]).each{
            lista << it
        }
        Aplicacao.executeQuery("select u.aplicacao.menu from UsuarioAplicacao u where u.usuario.id = ?", [this.id]).each{
            lista << it
        }
        return lista.sort{ it.tx_descr }
    }

}
